﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Rotempco.ORM.Generator.Objects;
using Microsoft.Win32;
using Rotempco.ORM.Generator.Logic;
using System.IO;

namespace Rotempco.ORM.Generator
{
    /// <summary>
    /// Interaction logic for SerializableObjectEditor.xaml
    /// </summary>
    public partial class SerializableObjectEditor : Window
    {
        private string _TableName;
        private DatabaseColumn _DatabaseColumn;
        private RotempcoSettings _ProjectSettings;

        public string SerializableObjectName { get; set; }
        public string SerializableObjectNamespace { get; set; }

        public SerializableObjectEditor()
        {
            InitializeComponent();
        }

        internal void SetSettings(Objects.RotempcoSettings rotempcoSettings, string tableName, DatabaseColumn databaseColumn)
        {
            _ProjectSettings = rotempcoSettings;
            _TableName = tableName;
            _DatabaseColumn = databaseColumn;

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "SerializableObject files|*.cs";
            if (openFileDialog.ShowDialog() == true)
            {
                LoadSerializableObject(openFileDialog.FileName);
                Close();
            }
        }

        private void LoadSerializableObject(string serializableObjectPath)
        {
            string serializableObjectContent = File.ReadAllText(serializableObjectPath);

            int afterNamespaceStart = serializableObjectContent.IndexOf("namespace ") + 10;
            int namespaceSerializableObject = serializableObjectContent.IndexOf(Environment.NewLine, afterNamespaceStart);

            string serializableObjectNameSpace = serializableObjectContent.Substring(afterNamespaceStart, namespaceSerializableObject - afterNamespaceStart);

            int afterSerializableObjectStart = serializableObjectContent.IndexOf(" class ") + 7;
            int serializableObjectNameStart = serializableObjectContent.IndexOf(Environment.NewLine, afterSerializableObjectStart);

            string serializableObjectName = serializableObjectContent.Substring(afterSerializableObjectStart, serializableObjectNameStart - afterSerializableObjectStart);

            string serializableObjectIdentifier = _TableName + "_" + _DatabaseColumn.Name;

            string projectNamespace = _ProjectSettings.SettingsFile.Substring(0, _ProjectSettings.SettingsFile.LastIndexOf('\\'));

            string serializableObjectSavePath_ForProjectsFile = serializableObjectPath.Substring(projectNamespace.Length + 1);

            
            SerializableObjectName = serializableObjectName;
            SerializableObjectNamespace = serializableObjectNameSpace;

            _ProjectSettings.SetKey("@SerializableObjectDefinitions_" + serializableObjectIdentifier, serializableObjectName);
            _ProjectSettings.SetKey("@SerializableObjectDefinitions_Path_" + serializableObjectIdentifier, serializableObjectSavePath_ForProjectsFile);
            _ProjectSettings.SetKey("@SerializableObjectDefinitions_Namespace_" + serializableObjectIdentifier, serializableObjectNameSpace);

            //string projectNamespaceProject = FileNameLogic.GetProjectNamespace(_ProjectSettings);

            //string serializableObjectSavePath_ForProjectsFileProjectFile = serializableObjectSavePath_ForProjectsFile.Replace(projectNamespaceProject + "\\", "");

            //ProjectFileLogic.InsertAndSaveElementToProject(_ProjectSettings, new FileInfo(_ProjectSettings.SettingsFile).Directory.FullName, serializableObjectSavePath_ForProjectsFileProjectFile);

        }
    }
}
